<?php

# function that will return data for selected group
function c_get_group_data(){
	$group_data = array();
	$group_id = intval($_GET['grp']);
	$user_id = $_SESSION['user_id'];
	if( c_is_participant($group_id, $user_id) ){ // display data only if user is participant of selected group
		if( !has_errors() ){
			# get group info
			$query = "SELECT g.NAME, g.DESC, g.MASTER FROM groups g WHERE ID = '$group_id' ";
			$result = c_db_query($query);

			if($result){
				$row = mysql_fetch_array($result);
				$group_data['name'] = $row[0];
				$group_data['desc'] = $row[1];
				$group_data['master_id'] = $row[2];
				$master_id = $row[2]; // second to have variable without quotes
			}
			else i_set_msg_err('Error in database 1');
		}
		
		if( !has_errors() ){
			# get master name
			$query = "SELECT NAME FROM user
						WHERE ID = '$master_id' ";
			$result = c_db_query($query);

			if($result){
				$row = mysql_fetch_array($result);
				$group_data['master_name'] = $row[0];
			}
			else i_set_msg_err('Error in database 2');
		}
		
		if( !has_errors() ){
			# get list of participants
			$query = "SELECT u.NAME FROM participants p 
						JOIN user u ON p.USER_ID = u.ID
						WHERE p.GROUP_ID = '$group_id' AND p.USER_ID != '$master_id' AND p.STATUS = '1' "; // TODO show not just name but also e-mail 
			$result = c_db_query($query);

			if($result){
				$participants = array();
				while($row = mysql_fetch_array($result)) 
				{
					$participants[] = $row[0];
				}
				$group_data['participants'] = $participants;
			}
			else i_set_msg_err('Error in database 3');
		}
		
		if( !has_errors() ){
			# get list of participant proposals
			$query = "SELECT u.NAME, u.ID FROM participants p 
						JOIN user u ON p.USER_ID = u.ID
						WHERE p.GROUP_ID = '$group_id' AND p.USER_ID != '$master_id' AND p.STATUS = '0' "; // TODO show not just name but also e-mail 
			$result = c_db_query($query);

			if($result){
				$proposals = array();
				while($row = mysql_fetch_array($result)) 
				{
					$proposals[] = array('part_name' => $row[0], 'part_id' => $row[1]);
				}
				$group_data['proposals'] = $proposals;
			}
			else i_set_msg_err('Error in database 4');
		}
		
		if( !has_errors() ){
			# get list of assigned meetings
			$query = "SELECT PLACE, S_TIME, E_TIME FROM def_meeting
						WHERE GROUP_ID = '$group_id' AND S_TIME > NOW()";
			$result = c_db_query($query);

			if($result){
				$meetings = array();
				while($row = mysql_fetch_array($result)) 
				{
					$row_time = c_calc_time($row[1], $row[2]);
					$meetings[] = array('place' => $row[0], 'duration' => $row_time['duration'], 
						'date' => $row_time['date'], 'time' => $row_time['time']);
				}
				$group_data['def_meetings'] = $meetings;
			}
			else i_set_msg_err('Error in database 5');
		}
		
		if( !has_errors() ){
			# get list of proposed meetings
			$query = "SELECT ID, PLACE, MIN_TIME, MAX_TIME, DURATION FROM prop_meeting
						WHERE GROUP_ID = '$group_id' AND MAX_TIME > NOW()";
			$result = c_db_query($query);

			if($result){
				$meetings = array();
				while($row = mysql_fetch_array($result)) 
				{	
					$status = false;
					# get status with this meeting
					if( $master_id != $_SESSION['user_id'] ){
						$meeting_id = $row[0];
						
						$query2 = "SELECT ID FROM prop_participants
							WHERE MEET_ID = '$meeting_id' AND USER_ID = ".$_SESSION['user_id'];
						$result2 = c_db_query($query2);
						
						if($result2){
							$nr_rows = mysql_num_rows($result2);
							if( $nr_rows == 1 ) $status = true;
						}
					}
					
					$meetings[] = array('meet_id' => $row[0], 'place' => $row[1], 'duration' => $row[4], 
						'min_date' => $row[2], 'max_date' => $row[3], 'status' => $status);
				}
				$group_data['prop_meetings'] = $meetings;
			}
			else i_set_msg_err('Error in database 6');
		}
	}
	else{
		i_set_msg_err('You are not participant of selected group.');
	}
	return $group_data;
} 

# function that checks whether user is participant of certain group
function c_is_participant($group_id, $user_id){
	$res = false;
	$query = "SELECT ID FROM participants WHERE USER_ID = $user_id AND GROUP_ID = $group_id";  
	$result = c_db_query($query);
	
	if($result){
		while($row = mysql_fetch_array($result)) 
		{
			$grpArr[] = array('id' => $row[0]);
		}
	}
	if( !empty($grpArr) ) $res = true;
	else $res = false;
	return $res;
}
?>






	 